Conditions | 10 |
Total Lines | 21 |
Code Lines | 14 |
Lines | 21 |
Ratio | 100 % |
Changes | 0 |
Complex classes like _script.js ➔ search often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | View Code Duplication | $('i.glyphicon-refresh-animate').hide(); |
|
29 | function search(target) { |
||
30 | var $list = $('select.list[data-target="' + target + '"]'); |
||
31 | $list.html(''); |
||
32 | var q = $('.search[data-target="' + target + '"]').val(); |
||
33 | |||
34 | var groups = { |
||
35 | role: [$('<optgroup label="Roles">'), false], |
||
36 | permission: [$('<optgroup label="Permission">'), false], |
||
37 | }; |
||
38 | $.each(_opts.items[target], function (name, group) { |
||
39 | if (name.indexOf(q) >= 0) { |
||
40 | $('<option>').text(name).val(name).appendTo(groups[group][0]); |
||
41 | groups[group][1] = true; |
||
42 | } |
||
43 | }); |
||
44 | $.each(groups, function () { |
||
45 | if (this[1]) { |
||
46 | $list.append(this[0]); |
||
47 | } |
||
48 | }); |
||
49 | } |
||
50 | |||
54 |